Vector Class
A 2d Vector implementation stolen directly from mrdoob's THREE.js Vector2d
Constructor
Vector
-
x -
y
Parameters:
-
xNumberThe x component of the vector
-
yNumberThe y component of the vector
Methods
add
-
vector
Adds a vector to this one
Parameters:
-
vectorVectorThe vector to add to this one
Returns:
Returns itself.
addScalar
-
scalar
Adds a scalar value to the x and y components of this vector
Parameters:
-
scalarNumberThe scalar value to add
Returns:
Returns itself.
addVectors
-
vector1 -
vector2
Adds two vectors to each other and stores the result in this vector
Returns:
Returns itself.
clone
()
Vector
Creates a new instance of Vector, with the same components as this vector
Returns:
Returns a new Vector with the same values
copy
-
vector
Copies the passed vector's components to this vector
Parameters:
-
vectorVectorThe vector to copy the values from
Returns:
Returns itself.
distanceTo
-
vector
Calculates the distance to the passed vector
Parameters:
-
vectorVectorThe vector to check distance to
Returns:
The distance
distanceToSquared
-
vector
Calculates the square distance to the passed vector
Parameters:
-
vectorVectorThe vector to check distance to
Returns:
The square distance
divideScalar
-
scalar
Divides the x and y components of this vector by a scalar value
Parameters:
-
scalarNumberThe value to divide by
Returns:
Returns itself.
dot
-
vector
Performs the dot product between this vector and the passed one and returns the result
Parameters:
-
vectorVector
Returns:
Returns the dot product
equals
-
vector
Checks if this vector is equal to another
Parameters:
-
vectorVectorThe vector to compare with
Returns:
Returns itself.
getComponent
-
index
Gets a component value of the vector
Parameters:
-
indexNumberThe index of the component to set (0 = x, 1 = y)
Returns:
Returns the component value
length
()
Number
Calculates the length of the vector
Returns:
Returns the length of the vector
lengthSq
()
Number
Calculates the square length of the vector
Returns:
Returns the square length of the vector
lerp
-
vector -
alpha
Performs a linear interpolation between this vector and the passed vector
Parameters:
-
vectorVectorThe vector to interpolate with
-
alphaNumberThe amount to interpolate [0-1] or extrapolate (1-]
Returns:
Returns itself.
max
-
vector
Sets this vector components to the maximum value when compared to the passed vector's components
Parameters:
-
vectorVectorThe vector to compare to
Returns:
Returns itself.
max
-
min -
max
Clamps the vectors components to be between min and max
Parameters:
Returns:
Returns itself.
min
-
vector
Sets this vector components to the minimum value when compared to the passed vector's components
Parameters:
-
vectorVectorThe vector to compare to
Returns:
Returns itself.
multiplyScalar
-
scalar
Multiplies the x and y components of this vector by a scalar value
Parameters:
-
scalarNumberThe value to multiply by
Returns:
Returns itself.
normalize
()
Vector
Normalizes this vector (divides by its length)
Returns:
Returns the normalized vector
rotate
-
angle -
anchor
Rotates the vector by an arbitrary angle around an arbitrary point in space
Parameters:
-
angleNumberThe angle in radians to rotate by
-
anchorVectorThe anchor point to rotate around
Returns:
Returns itself.
set
-
x -
y
Sets the value of the vector
Parameters:
-
xNumberThe x component of the vector
-
yNumberThe y component of the vector
Returns:
Returns itself.
setComponent
-
index -
value
Sets a component value of the vector
Parameters:
-
indexNumberThe index of the component to set (0 = x, 1 = y)
-
valueNumberThe value to set the component to
Returns:
Returns itself.
setLength
-
length
Sets the length of the vector
Parameters:
-
lengthNumberThe length to set this vector to
Returns:
Returns itself.
setX
-
x
Sets the X value of the vector
Parameters:
-
xNumberThe x component of the vector
Returns:
Returns itself.
setY
-
y
Sets the Y value of the vector
Parameters:
-
yNumberThe y component of the vector
Returns:
Returns itself.
sub
-
vector
Subtracts a vector from this one
Parameters:
-
vectorVectorThe vector to subtract from this one
Returns:
Returns itself.